<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <code-tech.diandian.com>
// +----------------------------------------------------------------------

namespace App\Controller;
use Think\Controller;
use OT;
use User\Api\UserApi;

//货主
class ConsignorController extends AppController{
   
    //得到货主常用地址
    public function getCommonAddress(){
        $map['status'] = 1;//正常状态
        $commonAddress = D("CommonAddress");
        $rst['rows'] = $commonAddress->where($map)->select();
        $this->ajaxReturn($rst);
    }
    //货主登陆
    public function login($username = null, $password = null, $verify = null){
        if(IS_POST){

            /* 调用UC登录接口登录 */
            $User = new UserApi;
            $uid = $User->login($username, $password);
            $consignor = D("Consignor");
            $user = $consignor->find($uid);
            if(0 < $uid){ //UC登录成功(账号密码验证成功)
                $data['success'] = true;
                $data['msg']     = '登陆成功';
                $data['obj'] = $user;
                $this->ajaxReturn($data);
            } else { //登录失败

                switch($uid) {
                    case -1: $error = '用户不存在或被禁用！'; break; //系统级别禁用
                    case -2: $error = '密码错误！'; break;
                    default: $error = '未知错误！'; break; // 0-接口参数错误（调试阶段使用）
                }
                $data['success'] = 0;
                $data['msg']     = $error;
                $this->ajaxReturn($data);
            }

        } else {

        }
    }
    public function register($username,$password,$repassword){
        if(IS_POST){
            /* 检测密码 */
            if($password != $repassword){
                $this->error('密码和重复密码不一致！');
            }

            /* 调用注册接口注册用户 */
            $User   =   new UserApi;
            $uid    =   $User->appregister($username, $password);
            if($uid >0){
                $consignor = D("Consignor");
                $user = $consignor->find($uid);
                $data['success'] = true;
                $data['msg']     = '注册成功';
                $data['obj'] = $user;
                $this->ajaxReturn($data);
            }else{
                $data['success'] = 0;
                $data['msg']     = $this->showRegError($uid);
                $this->ajaxReturn($data);
            }
        }

    }
    /**
     * 获取用户注册错误信息
     * @param  integer $code 错误编码
     * @return string        错误信息
     */
    private function showRegError($code = 0){
        switch ($code) {
            case -1:  $error = '用户名长度必须在16个字符以内！'; break;
            case -2:  $error = '用户名被禁止注册！'; break;
            case -3:  $error = '用户名被占用！'; break;
            case -4:  $error = '密码长度必须在6-30个字符之间！'; break;
            case -5:  $error = '邮箱格式不正确！'; break;
            case -6:  $error = '邮箱长度必须在1-32个字符之间！'; break;
            case -7:  $error = '邮箱被禁止注册！'; break;
            case -8:  $error = '邮箱被占用！'; break;
            case -9:  $error = '手机格式不正确！'; break;
            case -10: $error = '手机被禁止注册！'; break;
            case -11: $error = '手机号被占用！'; break;
            default:  $error = '未知错误';
        }
        return $error;
    }
    //得到货主订单表
    public function addConsignorOrder(){

        $order = D("Order");
        $orderList['consignorid']        = I('consignorid');
        $orderList['delivery_address']   = I('delivery_address');
        $orderList['delivery_latitude']  = I('delivery_latitude');
        $orderList['starting_time_of_pickup_time']  =  I('starttime');
        $orderList['pick_up_time_the_time_of_termination']  =  I('exittime');
        $orderList['receive_number']     = I('index');
        $orderList['order_status']       = 0;//表示系统已经接单但是未分配司机
        $order->startTrans();
        $orderRst = $order->add($orderList);
        if(!$orderRst){
            $order->rollback();
            $data['success'] = 0;
            $data['msg']     = "订单提交失败";
            $this->ajaxReturn($data);
        }
        //发货地址存储
        $faList['receiving_fixed_address']      = I('shipping_address');
        $faList['receiving_detailed_address']   = I('shipping_detailed_address');
        $faList['consignee_name']               = I('common_name');
        $faList['consignee_cell phone']         = I('commonly_used_mobile_phone');
        $faList['address_type']                 = 0;
        $shippingAddressList = D('ShippingAddress');
        $AddressRst = $shippingAddressList->add($faList);
        if(!$AddressRst){
            $order->rollback();
            $data['success'] = 0;
            $data['msg']     = "订单提交失败";
            $this->ajaxReturn($data);
        }
        for($i=1;$i<=(I('index')+1);$i++){
            $shouList['receiving_fixed_address']      = I('shipping_address'.$i);
            $shouList['receiving_detailed_address']   = I('shipping_detailed_address'.$i);
            $shouList['consignee_name']               = I('common_name'.$i);
            $shouList['consignee_cell phone']         = I('commonly_used_mobile_phone'.$i);
            $shouList['description_of_every_goods']   = I('good_context'.$i);
            $shouList['good_number']                  = I('number'.$i);
            $shouList['every_volume_of_goods']        = I('volume'.$i);
            $shouList['address_type']                 = 1;
            $shouList['service_type']                 = I('service'.$i);
            $shippingAddressListRst = $shippingAddressList->add($shouList);
            if(!$shippingAddressListRst){
                $order->rollback();
                $data['success'] = 0;
                $data['msg']     = "订单提交失败";
                $this->ajaxReturn($data);
            }
        }
        $order->commit();
        $data['success'] = 1;
        $data['msg']     = "订单提交成功";

        $this->ajaxReturn($data);

    }
    //得到手机端的经纬度来计算距离和价钱
    public function getDistanceAndPrice(){
        
    }
}